package com.baosight.sczx.zm.cg.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cg.domain.ZMCGContactReceive;
import com.baosight.sczx.zm.cg.domain.ZMCGDutyschedule;
import com.baosight.sczx.zm.cg.domain.ZMCGDutyscheduleDeploy;
import com.baosight.sczx.zm.domain.ZM03;
/**
 * 排班
 * <p>Title: ServiceZMCG50.java</p> 
 */
public class ServiceZMCG50 extends ServiceEPBase {

	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo); 
	  
		return outInfo;
	}

	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) { 
		StaticTools.setQueryParameters(inInfo, "fdMiningId",UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGDutyscheduleDeploy.query", new ZMCGDutyscheduleDeploy());
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		return outInfo;
	}
	/**
	 * <p>Function: insert</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGDutyscheduleDeploy _dom = new ZMCGDutyscheduleDeploy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdBeginDate",_dom.getFdBeginDate());
			params.put("fdEndDate",_dom.getFdEndDate());
			
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate){
				return inInfo;
			}else{
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_DUTYSCHEDULE_DEPLOY"));
			this.dao.insert("ZMCGDutyscheduleDeploy.insert",_dom);
		}
	}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	/**
	 * <p>Function: update</p> 
	 */
	@Override	
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGDutyscheduleDeploy _dom = new ZMCGDutyscheduleDeploy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate){
				return inInfo;
			}else{
			this.dao.update("ZMCGDutyscheduleDeploy.update",_dom);
		}
	}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
}
	/**
	 * <p>Function: delete</p> 
	 */
	@Override	
	public EiInfo delete(EiInfo inInfo) { 
		EiInfo _temp = super.delete(inInfo, "ZMCGDutyscheduleDeploy.delete");
		EiInfo out=new EiInfo(); 
		out = this.query(inInfo);  
		out.setMsg(_temp.getMsg());
		out.setDetailMsg(_temp.getDetailMsg());
		return out; 
	}
	/**
	 * <p>Function: excute</p> 
	 */ 
	public EiInfo excute(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGDutyscheduleDeploy _dom = new ZMCGDutyscheduleDeploy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			
			String[] mineLeaders=_dom.getFdMiningLeader().split(",");
			String[] ddLeaders=_dom.getFdDdLeader().split(",");
			String[] ddPersons=_dom.getFdDdPerson().split(",");  
			//获取上个班次的所有人员信息，包括矿领导，调度主任，调度人员信息，然后对当前的人员列表进行排序，保证不和上个班次的人员信息重复
			HashMap<String,String> hs=new HashMap<String,String>();
			hs.put("fdVirtualDate", StaticTools.getYesterday(_dom.getFdBeginDate()));
			hs.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			List<ZMCGDutyschedule> _list = dao.query("ZMCGDutyschedule.query", hs); 
			if(_list.size()>0)
			{
				String lastMineLeader=_list.get(0).getFdLeaderName();
				String lastDdLeader=_list.get(0).getFdDdzrName();
				String lastDdPerson=_list.get(0).getFdDdwanName();
				if(lastMineLeader==mineLeaders[0])
				{
					String tmpStr = mineLeaders[0];
					mineLeaders[1]=mineLeaders[0];
					mineLeaders[0]=tmpStr;
				}
				if(lastDdLeader==ddLeaders[0])
				{
					String tmpStr = ddLeaders[0];
					ddLeaders[1]=ddLeaders[0];
					ddLeaders[0]=tmpStr;
				}
				if(lastDdPerson==ddPersons[0])
				{
					String tmpStr = ddPersons[0];
					ddPersons[1]=ddPersons[0];
					ddPersons[0]=tmpStr;
				}
			} 
			//判断起始日期和截止日期--用遍历
			String tmpDate=_dom.getFdBeginDate();
			//直接删除当天日期以后的排版记录，然后重新执行排版
			hs=new HashMap<String,String>();
			hs.put("fdBeginDate",_dom.getFdBeginDate());
			hs.put("fdEndDate",_dom.getFdEndDate());
			hs.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			dao.delete("ZMCGDutyschedule.deleteInvalidDutyschedule", hs); 
			
			
			int tmpMineLeaderIndex=0;
			int tmpDdLeader=0;
			int tmpPersonIndex=0;
			ZMCGDutyschedule zds;
			while(tmpDate.compareTo(_dom.getFdEndDate())<=0){  
//				if(tmpDate.equals(_dom.getFdEndDate())){ 
//					break;
//				} 
				zds=new ZMCGDutyschedule();
				zds.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_DUTYSCHEDULE"));
				zds.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				zds.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				zds.setFdCreateUserId(UserSession.getUserId().toString());
				zds.setFdCreateUser(UserSession.getInSessionProperty("UserCname").toString());
				zds.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
				zds.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
				zds.setFdEditDate(StaticTools.getDate());
				zds.setFdVirtualDate(tmpDate);
				zds.setFdLeaderName(mineLeaders[tmpMineLeaderIndex]);
				if(tmpDdLeader==ddLeaders.length-1)tmpDdLeader=0;
				else tmpDdLeader++;
				zds.setFdDdzrName(ddLeaders[tmpDdLeader]); 
				if(tmpMineLeaderIndex==mineLeaders.length-1)tmpMineLeaderIndex=0;
				else tmpMineLeaderIndex++;
				zds.setFdDdzaoName(ddPersons[tmpPersonIndex]);
				if(tmpPersonIndex==ddPersons.length-1)tmpPersonIndex=0;
				else tmpPersonIndex++;  
				zds.setFdDdzhongName(ddPersons[tmpPersonIndex]);
				if(tmpPersonIndex==ddPersons.length-1)tmpPersonIndex=0;
				else tmpPersonIndex++;  
				zds.setFdDdwanName(ddPersons[tmpPersonIndex]);
				if(tmpPersonIndex==ddPersons.length-1)tmpPersonIndex=0;
				else tmpPersonIndex++;  
				this.dao.insert("ZMCGDutyschedule.insert",zds); 
				tmpDate=StaticTools.getTomorrow(tmpDate); 
			} 
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	public EiInfo queryMineLeaderBlock(EiInfo inInfo)
	{
		EiInfo outInfo = super.query(inInfo, "ZM03.query", new ZM03());
		return outInfo;
	}
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCGDutyscheduleDeploy _dom, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdBeginDate = _dom.getFdBeginDate();
		String fdEndDate = _dom.getFdEndDate();
		
		params.put("fdBeginDate", fdBeginDate);
		params.put("fdEndDate", fdEndDate);
		
		List result = dao.query("ZMCGDutyscheduleDeploy.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCGDutyscheduleDeploy _temp = (ZMCGDutyscheduleDeploy)result.get(0);
		
			StringBuffer msg = new StringBuffer("提交失败，");
			if((_temp.getFdBeginDate() != null && _temp.getFdBeginDate().equals(fdBeginDate)) && (_temp.getFdEndDate() != null && _temp.getFdEndDate().equals(fdEndDate)) ) {
				msg.append(" 起始结束时间与其他记录 ");
			}

		msg.append("重复！");
		
		inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
}
